package com.chzu.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州大健康与养老产业研究院
 * @ClassName SysUser
 * @Description 用户信息实体类
 * @Date 2022/01/27 14:27
 */
@Data
public class SysUser implements Serializable, UserDetails {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id",type = IdType.AUTO)
    private Integer id;

    @ApiModelProperty(value = "用户名")
    private String userName;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "性别")
    private Integer sex;

    @ApiModelProperty(value = "昵称")
    private String nickName;

    @ApiModelProperty(value = "状态")
    private boolean status;

    @ApiModelProperty(value = "微信获取用户头像")
    private String avatar;

    @ApiModelProperty(value = "微信小程序用户唯一标识")
    private String openId;

    @ApiModelProperty("微信获取用户地址")
    private String address;

    @ApiModelProperty("是否是管理员")
    private boolean admin;

    @ApiModelProperty(value = "电话号码")
    private String phoneNumber;

    @ApiModelProperty(value = "用户的角色")
    @TableField(exist = false)
    private List<SysRole> roles;

    @ApiModelProperty(value = "用户对应的菜单")
    @TableField(exist = false)
    private List<SysMenu> menus;

    @ApiModelProperty(value = "用户对应数据权限")
    @TableField(exist = false)
    private List<SysPermission> permissions;

    /**
     * 权限数据
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> list = new ArrayList<>();
        // 设置角色
        roles.forEach(item -> {
            if (item.getCode() != null) {
                list.add(new SimpleGrantedAuthority("ROLE_" + item.getCode()));
            }
        });
        // 设置权限
        if(permissions != null && permissions.size() > 0) {
            permissions.forEach(item -> {
                if (item.getCode() != null) {
                    list.add(new SimpleGrantedAuthority(item.getCode()));
                }
            });
        }
        return list;
    }

    /**
     * 用户名
     * @return
     */
    @Override
    public String getUsername() {
        return userName;
    }


    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    /**
     * 账户是否被锁定
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    /**
     * 是否被禁用
     * @return
     */
    @Override
    public boolean isEnabled() {
        return status;
    }
}
